﻿using System; 
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

namespace QMesSocketLib
{
    public class Log
    {
        public static string  AppName
        {
             get;
             set;
        }
        private Log()
        { }


        /// <summary>
        /// 发布异常信息。
        /// </summary>
        /// <param name="exception"></param>
        public static void LogError(Exception exception)
        {
            try
            {
                StringBuilder strInfo = new StringBuilder();
                strInfo.Append("\r\n---" + DateTime.Now.ToString() + "---\r\n");
                strInfo.Append("错误列表:\r\n");

                int exCount = 1;
                Exception currException = exception;
                do
                {
                    strInfo.Append(exCount + ") ");
                    strInfo.Append(currException.GetType().Name);
                    strInfo.Append("\r\n信息: ");
                    strInfo.Append(currException.Message);
                    strInfo.Append("\r\n");
                    exCount++;
                    currException = currException.InnerException;
                }
                while (currException != null);
                strInfo.Append("\r\n" + exception.StackTrace);

                string logFolder = "ErrorLog/";
                CreateFolder(logFolder);
                string logFile = logFolder + AppName+DateTime.Now.ToString("yyyy-MM-dd") + ".log";
                logFile = Path.Combine(System.IO.Directory.GetCurrentDirectory(), logFile);
                
                using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8))
                {
                    sw.Write(strInfo.ToString());
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }

        public static void LogError(Exception exception,string msg)
        {
            try
            {
                StringBuilder strInfo = new StringBuilder();
                strInfo.Append("\r\n---" + DateTime.Now.ToString() + "---\r\n");
                strInfo.Append("备注:" + msg + "\r\n");
                strInfo.Append("错误列表:\r\n");

                int exCount = 1;
                Exception currException = exception;
                do
                {
                    strInfo.Append(exCount + ") ");
                    strInfo.Append(currException.GetType().Name);
                    strInfo.Append("\r\n信息: ");
                    strInfo.Append(currException.Message);
                    strInfo.Append("\r\n");
                    exCount++;
                    currException = currException.InnerException;
                }
                while (currException != null);
                strInfo.Append("\r\n" + exception.StackTrace);

                string logFolder = "ErrorLog/";
                CreateFolder(logFolder);
                string logFile = logFolder + AppName + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
                logFile = Path.Combine(System.IO.Directory.GetCurrentDirectory(), logFile);

                using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8))
                {
                    sw.Write(strInfo.ToString());
                }
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }
        /// <summary>
        /// 发布异常信息。
        /// </summary>
        /// <param name="exception"></param>
        public static void LogErrorPC(Exception exception)
        {
            try
            {
                StringBuilder strInfo = new StringBuilder();
                strInfo.Append("\r\n---" + DateTime.Now.ToString() + "---\r\n");
                strInfo.Append("错误列表:\r\n");

                int exCount = 1;
                Exception currException = exception;
                do
                {
                    strInfo.Append(exCount + ") ");
                    strInfo.Append(currException.GetType().Name);
                    strInfo.Append("\r\n信息: ");
                    strInfo.Append(currException.Message);
                    strInfo.Append("\r\n");
                    exCount++;
                    currException = currException.InnerException;
                }
                while (currException != null);
                strInfo.Append("\r\n" + exception.StackTrace);

                string logFolder = "ErrorLog/";
                CreateFolder(logFolder);
                string logFile = logFolder + AppName+DateTime.Now.ToString("yyyy-MM-dd") + ".log";

                logFile = Path.Combine(System.IO.Directory.GetCurrentDirectory(), logFile);
                using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8))
                {
                    sw.Write(strInfo.ToString());
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }

        /// <summary>
        /// 记载调试信息
        /// </summary>
        /// <param name="strLog">要写入的调试字符串</param>
        public static void LogMessage(string strLog)
        {
            try
            {
                string logFolder = "ErrorLog/";
                CreateFolder(logFolder);
                string logFile = logFolder + AppName+DateTime.Now.ToString("yyyy-MM-dd") + ".log";
               logFile = Path.Combine(System.IO.Directory.GetCurrentDirectory(), logFile);
                
                string log = DateTime.Now.ToString() + " " + strLog;
                using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8))
                {
                    sw.WriteLine(log);
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
            catch (Exception ex)
            {
                //throw ex;
            }

        }

        /// <summary>
        /// 在程序当前目录下新建文件夹，如果已存在则不作任何操作
        /// </summary>
        /// <param name="folderName">文件夹名称</param>
        public static void CreateFolder(string folderName)
        {
            try
            {
                string fullPath = string.Empty;
                fullPath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), folderName);                
                if (!Directory.Exists(fullPath))
                {
                    Directory.CreateDirectory(fullPath);
                }
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }
    }
}
